﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DotNetDalAddin.Properties;

namespace DotNetDalAddin
{
    public partial class Configure : Form
    {
        private Generator parent;
        private string originalSqlString;

        public Configure(Generator _parent)
        {
            InitializeComponent();

            parent = _parent;

            loadSettings();
        }

        private void loadSettings()
        {
            tbxDataFolderName.Text = Properties.Settings.Default.AbstractFolderName;
            tbxBusinessFolderName.Text = Properties.Settings.Default.BusinessFolderName;
            tbxSqlConnString.Text = originalSqlString = Properties.Settings.Default.SqlConnection;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.AbstractFolderName = tbxDataFolderName.Text;
            Properties.Settings.Default.BusinessFolderName = tbxBusinessFolderName.Text;
            Properties.Settings.Default.SqlConnection = tbxSqlConnString.Text;

            if (Properties.Settings.Default.SqlConnection != originalSqlString)
                parent.RefreshTree();

            Properties.Settings.Default.Save();

            this.Close();
        }

        private void btnSetSqlConn_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.SqlConnection = "";

            while (Properties.Settings.Default.SqlConnection == "")
            {
                parent.SetSqlConnectionString();

                if (Properties.Settings.Default.SqlConnection == "")
                    MessageBox.Show("Please choose a valid connection");
                else
                    tbxSqlConnString.Text = Properties.Settings.Default.SqlConnection;
            }

            DotNetDal.Core.SqlServer.RegisterSqlConnectionString(Settings.Default.SqlConnection);
        }
    }
}
